package com.androidWAC;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_LOCATION = "location";
    public static final String KEY_MONTH = "month";
    public static final String KEY_DAY = "day";
    public static final String KEY_YEAR = "year";
    public static final String KEY_STARTHOUR = "starthour";
    public static final String KEY_STARTMINUTE = "startminute";
    public static final String KEY_ENDHOUR = "endhour";
    public static final String KEY_ENDMINUTE = "endminute";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "ReminderGenie";
    private static final String DATABASE_TABLE = "Events";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table Events (_id integer primary key autoincrement, "
        + "title text not null, location text not null, "
        + "month integer not null, day integer not null, "
        + "year integer not null, starthour integer not null, "
        + "startminute integer not null, endhour integer not null,"
        + "endminute integer not null);";

    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
                              int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion
                  + " to "
                  + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }
  //---opens the database---
    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---
    public void close()
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertEvent(String title, String location, int month, int day, 
    		int year, int starthour, int startminute, int endhour, int endminute)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_LOCATION, location);
        initialValues.put(KEY_MONTH, month);
        initialValues.put(KEY_DAY, day);
        initialValues.put(KEY_YEAR, year);
        initialValues.put(KEY_STARTHOUR, starthour);
        initialValues.put(KEY_STARTMINUTE, startminute);
        initialValues.put(KEY_ENDHOUR, endhour);
        initialValues.put(KEY_ENDMINUTE, endminute);
        
        System.out.println(db);
        System.out.println("hello");
        long temp = db.insert(DATABASE_TABLE, null, initialValues);
        return temp;
    }

    //---deletes a particular title---
    public boolean deleteEvent(long rowId)
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    //---retrieves all the events---
    public Cursor getAllEvents()
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID,
                KEY_TITLE,
                KEY_LOCATION,
                KEY_MONTH,
                KEY_DAY,
                KEY_YEAR,           
                KEY_STARTHOUR,
                KEY_STARTMINUTE,
                KEY_ENDHOUR,
                KEY_ENDMINUTE},
                null,
                null,
                null,
                null,
                null);
    }

    //---retrieves a particular event---
    public Cursor getEvent(long rowId) throws SQLException
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_TITLE,
                        KEY_LOCATION,
                        KEY_MONTH,
                        KEY_DAY,
                        KEY_YEAR,           
                        KEY_STARTHOUR,
                        KEY_STARTMINUTE,
                        KEY_ENDHOUR,
                        KEY_ENDMINUTE},
                        KEY_ROWID + "=" + rowId,
                        null,
                        null,
                        null,
                        null,
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates an event---
    public boolean updateEvent(long rowId, String title, String location, 
    		int month, int day, int year, int starthour, int startminute, 
    		int endhour, int endminute)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_LOCATION, location);
        args.put(KEY_MONTH, month);
        args.put(KEY_DAY, day);
        args.put(KEY_YEAR, year);        
        args.put(KEY_STARTHOUR, starthour);
        args.put(KEY_STARTMINUTE, startminute);
        args.put(KEY_ENDHOUR, endhour);
        args.put(KEY_ENDMINUTE, endminute);
        return db.update(DATABASE_TABLE, args,
                         KEY_ROWID + "=" + rowId, null) > 0;
    }


}
